<template>
  <div class="singer-table">
    <el-dialog
      :title="!singerForm.id ? '新增' : '修改'"
      :visible.sync="singerAddDialogVisible"
       width="50%" :center="true">
      <el-form :model="singerForm" ref="singerForm" label-width="80px">
          <el-form-item prop="name" label="歌手名" size="mini">
            <el-input v-model="singerForm.name" placeholder="歌手名"></el-input>
          </el-form-item>
        <el-form-item prop="sex" label="性别" size="mini">
          <el-radio-group v-model="singerForm.sex">
            <el-radio :label="0">女</el-radio>
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">组合</el-radio>
            <el-radio :label="3">不明</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="birth" label="生日" size="mini">
          <el-date-picker type="date" placeholder="选择日期" v-model="singerForm.birth" style="width: auto"></el-date-picker>
        </el-form-item>
        <el-form-item prop="pic" label="图片" size="mini">
          <singlePicUpload v-model="singerForm.pic" @input="inputHandler" ></singlePicUpload>
        </el-form-item>
        <el-form-item prop="location" label="地区" size="mini">
          <el-input v-model="singerForm.location" placeholder="地区"></el-input>
        </el-form-item>
        <el-form-item prop="introduction" label="介绍" size="mini">
          <el-input v-model="singerForm.introduction" placeholder="介绍" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" type="warning" @click="singerAddDialogVisible=false">取消</el-button>
        <el-button size="mini"  type="primary" @click="addSinger">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import singlePicUpload from '@/components/upload/singlePicUpload'
export default {
  name: 'singer-add-or-update',
  components: {
    singlePicUpload
  },
  data () {
    return {
      singerAddDialogVisible: false,
      singerForm: {
        id: '',
        name: '',
        sex: '',
        birth: '',
        pic: '',
        location: '',
        introduction: ''
      }
    }
  },
  methods: {
    inputHandler (url) {
      console.log(url)
      console.log(1111)
      this.singerForm.pic = url
    },
    init (id) {
      console.log(id)
      this.singerForm.id = id || 0
      this.singerAddDialogVisible = true
      this.$nextTick(() => {
        this.$refs['singerForm'].resetFields()
        if (this.singerForm.id) {
          this.$api.getSingerById({
            params: {id: id}
          }).then((res) => {
            console.log(res.data)
            if (res.data && res.data.code === 200) {
              console.log(22222)
              this.singerForm.name = res.data.data.name
              this.singerForm.pic = res.data.data.pic
              this.singerForm.sex = res.data.data.sex
              this.singerForm.birth = res.data.data.birth
              this.singerForm.location = res.data.data.location
              this.singerForm.introduction = res.data.data.introduction
            }
          })
        }
      })
    },
    addSinger () {
      let that = this
      let birth = this.singerForm.birth
      if (birth) {
        // birth = birth.getFullYear() + '-' + (birth.getMonth() + 1) + '-' + birth.getDate()
        birth = new Date(birth).getTime()
      }
      let params = {
        name: this.singerForm.name,
        sex: this.singerForm.sex,
        birth: birth,
        pic: this.singerForm.pic,
        location: this.singerForm.location,
        introduction: this.singerForm.introduction
      }
      if (!this.singerForm.id) {
        this.$api.addSinger(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.singerAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      } else {
        params.id = this.singerForm.id
        this.$api.updateSinger(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.singerAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
  .singer-table{
    margin: auto;
    width: 80%;
  }
  .singer-img{
    width: 100px;
    height: 100px;
    border-radius: 5px;
    overflow: hidden;
  }
  body .el-table th.gutter{
    display: table-cell!important;
  }

</style>
